//package com.bupt.ilink.service.studyaboutTeacherCourse;
//
//
//import com.bupt.ilink.entity.Studyspace.StudyCourseFile;
//import com.bupt.ilink.entity.Studyspace.StudyFileSource;
//import com.bupt.ilink.mapper.StudyCourseFileMapper;
//import com.bupt.ilink.mapper.StudyFileSourceMapper;
//import com.bupt.ilink.service.OssService;
//import com.bupt.ilink.service.bailian.FileService;
//import com.bupt.ilink.utils.R;
//import com.bupt.ilink.utils.SnowflakeIdGenerator;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.web.multipart.MultipartFile;
//
//import java.util.concurrent.LinkedBlockingQueue;
//import java.util.concurrent.ThreadPoolExecutor;
//import java.util.concurrent.TimeUnit;
//
//@Slf4j
//public class handleFileSource {
//    private final ThreadPoolExecutor taskExecutor;
//
//    private final FileService fileService;
//
//    private final OssService ossService;
//
//    private final StudyFileSourceMapper studyFileSourceMapper;
//
//    private final StudyCourseFileMapper studyCourseFileMapper;
//
//
//    public handleFileSource(
//                            OssService ossService,
//                            FileService fileService,
//                            StudyFileSourceMapper studyFileSourceMapper,
//                            StudyCourseFileMapper studyCourseFileMapper,
//                            int corePoolSize,
//                            int maxPoolSize,
//                            long keepAliveSeconds,
//                            int queueCapacity) {
//        this.ossService = ossService;
//        this.fileService = fileService;
//        this.studyFileSourceMapper = studyFileSourceMapper;
//        this.studyCourseFileMapper = studyCourseFileMapper;
//        // 动态传入配置参数
//        this.taskExecutor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveSeconds, TimeUnit.SECONDS, new LinkedBlockingQueue<>( queueCapacity));
////        this.uploadTaskQueue = threadPoolConfig.uploadTaskQueue();
//    }
//
//    //加工文档
//    public R handleFileTask(MultipartFile file, Long fileSourceId) {
//        try {
//
//            StudyFileSource fileSource = studyFileSourceMapper.selectById(fileSourceId);
//            String fileUrl = ossService.getFileUrl(fileSource.getFilesourcePath());
//            if(fileUrl == null){
//                return R.failure("文件未找到"+fileSource.getFileName());
//            }
//
//            // 将上传任务提交到线程池执行
//            taskExecutor.execute(() -> processFileHandle(file,fileSource, newFilename, courseSource));
//
//            return R.ok("文件已加入加工队列，稍后处理: " + file.getOriginalFilename());
//        } catch (Exception e) {
//            return R.failure("文件上传失败: " + file.getOriginalFilename(),e.getMessage());
//        }
//    }
//    // 处理文件加工
//    private void processFileHandle(MultipartFile file,StudyFileSource fileSource, StudyFileSource courseSource) {
//        try {
//            //RAG上传加工
//            // 找到最后一个斜杠的位置
//            int lastSlashIndex = fileSource.getFilesourcePath().lastIndexOf('/');
//
//            // 使用最后一个斜杠位置分割字符串
//            String filePath = fileSource.getFilesourcePath().substring(0, lastSlashIndex);
//            String newFilename = fileSource.getFilesourcePath().substring(lastSlashIndex + 1);
//            R response=fileService.uploadFile(file, filePath,newFilename);
//            if (response == null || response.getCode()==0) {
//                if (response != null) {
//                    fileSource.setHandleCompleteStatus(true);
//                    studyFileSourceMapper.updateById(fileSource);
//                    return R.failure("RAG文件上传失败: " + fileSource.getFileName()+" "+response.getMsg());
//                }else {
//                    fileSource.setHandleCompleteStatus(true);
//                    studyFileSourceMapper.updateById(fileSource);
//                    return R.failure("RAG文件上传失败: " + fileSource.getFileName());
//                }
//            }else {
//                fileSource.setRagCompleteStatus(true);
//                studyFileSourceMapper.updateById(fileSource);
//
//            }
//
//
//
//
//
//
//        }
//            boolean uploadSuccess = ossService.uploadFile(file, filePath, newFilename);
//            if (uploadSuccess) {
//                courseSource.setOssWriteStatus(true);
//                boolean updateSuccess =studyFileSourceMapper.updateById(courseSource)>0;
//                if (!updateSuccess) {
//                    log.error("课程:"+courseSource.getCourseId()+"中的文件上传成功，但更新数据库失败: " + file.getOriginalFilename());
//                    //需要进行回退处理删除
//                    ossService.deleteFile(newFilename,filePath);
//                    log.info("课程:"+courseSource.getCourseId()+"中的文件上传成功，但更新数据库失败，已进行OSS删除回退处理: " + file.getOriginalFilename());
//                }else {
//                    log.info("课程:"+courseSource.getCourseId()+"中的文件上传成功: " + file.getOriginalFilename());
//                }
//                // 更新 Redis 状态为 "上传成功"
////                redisTemplate.opsForHash().put("fileUploadStatus", courseSource.getFilesourceId().toString(), "上传成功");
//            } else {
//                log.error("课程:"+courseSource.getCourseId()+"中的文件上传失败: " + file.getOriginalFilename());
//                // 更新 Redis 状态为 "上传失败"
////                redisTemplate.opsForHash().put("fileUploadStatus", courseSource.getFilesourceId().toString(), "上传失败");
//            }
//        } catch (Exception e) {
//            // 更新 Redis 状态为 "上传失败"
////            redisTemplate.opsForHash().put("fileUploadStatus", courseSource.getFilesourceId().toString(), "上传失败");
//            log.error("课程:"+courseSource.getCourseId()+"中的文件上传中出现异常: " + file.getOriginalFilename(), e);
//        }
//}
